home *** CD-ROM | disk | FTP | other *** search
/ Pocket PC Game Programming / Pocket PC Game Programming.iso / source.exe / CH15 / GameLibrary / CAnimSprite.cpp next >
Encoding:
C/C++ Source or Header  |  2001-02-05  |  6.0 KB  |  232 lines

  1. //////////////////////////////////////////////////////////////////////
  2. // Pocket PC Game Programming
  3. // Chapter 9: Sprites and Animation
  4. //
  5. // CAnimSprite Source File
  6. //
  7. // This file includes the CAnimSprite class implementation.
  8. //
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. #include "stdafx.h"
  12. #include "CSprite.h"
  13. #include "CAnimSprite.h"
  14.  
  15. //////////////////////////////////////////////////////////////////////
  16. // CAnimSprite Constructor
  17. //
  18. // This function is called when the class is instantiated.
  19. //
  20. CAnimSprite::CAnimSprite(HDC hdc): CSprite(hdc)
  21. {
  22.     iCurFrame = 0;
  23.     iTotalFrames = 0;
  24.  
  25.     for (int n = 0; n < NUM_FRAMES; n++)
  26.         sprites[n] = new CBitmap(hdc);
  27.  
  28. }
  29.  
  30. //////////////////////////////////////////////////////////////////////
  31. // CAnimSprite Destructor
  32. //
  33. // This function is called when the class is terminated.
  34. //
  35. CAnimSprite::~CAnimSprite()
  36. {
  37.     for (int n = 0; n < NUM_FRAMES; n++)
  38.         delete sprites[n];
  39. }
  40.  
  41. //////////////////////////////////////////////////////////////////////
  42. // CAnimSprite::SetImageSize
  43. //
  44. // Sets the size of each animated sprite for loading.
  45. //
  46. void CAnimSprite::SetImageSize(int iImgWidth, int iImgHeight)
  47. {
  48.     iWidth = iImgWidth;
  49.     iHeight = iImgHeight;
  50. }
  51.  
  52. //////////////////////////////////////////////////////////////////////
  53. // CAnimSprite::LoadTile
  54. //
  55. // Loads sprite from a portion of a tiled bitmap image.
  56. //
  57. BOOL CAnimSprite::LoadTile(int iTileNum, LPTSTR lpFilename, int iWidth, int iHeight)
  58. {
  59.     CBitmap *source;
  60.     int iStartX, iStartY;
  61.  
  62.     //make sure tile number is inside range
  63.     if (iTileNum < 0 || iTileNum > NUM_FRAMES)
  64.         return FALSE;
  65.  
  66.     //make sure sprite is not too big
  67.     if (iWidth < 1 || iWidth > 240 || iHeight < 1 || iHeight > 320)
  68.         return FALSE;
  69.  
  70.     //load the source image
  71.     source = new CBitmap(GetScreenDC());
  72.     if (!source->Load(lpFilename))
  73.     {
  74.         delete source;
  75.         return FALSE;
  76.     }
  77.  
  78.     //calculate the tile position
  79.     iStartX = iTileNum * (iWidth + 1);
  80.     iStartY = 0;
  81.  
  82.     //create the new sprite frame
  83.     if (!sprites[iTileNum]->Create(iWidth, iHeight))
  84.     {
  85.         delete source;
  86.         return FALSE;
  87.     }
  88.  
  89.     //copy to the new sprite frame
  90.     BitBlt(sprites[iTileNum]->GetSourceDC(),
  91.         0, 0, iWidth, iHeight, 
  92.         source->GetSourceDC(),
  93.         iStartX, iStartY, SRCCOPY);
  94.  
  95.     //prepare new frame for use
  96.     iTotalFrames++;
  97.     SetFrame(iTileNum);
  98.     SetImageSize(iWidth, iHeight);
  99.     delete source;
  100.  
  101.     return TRUE;
  102. }
  103.  
  104. //////////////////////////////////////////////////////////////////////
  105. // CAnimSprite::LoadAnimSeq
  106. //
  107. // Loads an animation sequence from a bitmap into the frames of an
  108. // animated sprite. Columns and rows are frames, not pixels. Does 
  109. // not support wrapped animation sequences: make each sequence 
  110. // stay on a single row.
  111. //
  112. BOOL CAnimSprite::LoadAnimSeq(int iStartCol, int iNumFrames, int iRow, LPTSTR lpFilename, int iWidth, int iHeight)
  113. {
  114.     CBitmap *source;
  115.     int iStartX, iStartY, iFrame;
  116.  
  117.     if (iStartCol < 0 || iNumFrames > NUM_FRAMES)
  118.         return FALSE;
  119.  
  120.     if (iWidth < 1 || iWidth > 240 || iHeight < 1 || iHeight > 320)
  121.         return FALSE;
  122.  
  123.     source = new CBitmap(GetScreenDC());
  124.     if (!source->Load(lpFilename))
  125.     {
  126.         delete source;
  127.         return FALSE;
  128.     }
  129.  
  130.     for (iFrame = 0; iFrame < iNumFrames; iFrame++)
  131.     {
  132.         if (!sprites[iFrame]->Create(iWidth, iHeight))
  133.         {
  134.             delete source;
  135.             return FALSE;
  136.         }
  137.  
  138.         iStartX = iStartCol * (iWidth + 1) + iFrame * (iWidth + 1);
  139.         iStartY = iRow * (iHeight + 1);
  140.  
  141.         BitBlt(sprites[iFrame]->GetSourceDC(),
  142.             0, 0, iWidth, iHeight, 
  143.             source->GetSourceDC(),
  144.             iStartX, iStartY, SRCCOPY);
  145.  
  146.         iTotalFrames++;
  147.     }
  148.     
  149.     SetFrame(iStartCol);
  150.     SetImageSize(iWidth, iHeight);
  151.     delete source;
  152.  
  153.     return TRUE;
  154. }
  155.  
  156. //////////////////////////////////////////////////////////////////////
  157. // CAnimSprite::BitBlit
  158. //
  159. // Draws the sprite to the destination DC already specified.
  160. //
  161. BOOL CAnimSprite::BitBlit()
  162. {
  163.     return BitBlt(GetScreenDC(), GetX(), GetY(),
  164.         ImageWidth(), ImageHeight(), 
  165.         sprites[iCurFrame]->GetSourceDC(), 0, 0, SRCCOPY);
  166. }
  167.  
  168. //////////////////////////////////////////////////////////////////////
  169. // CAnimSprite::StretchBlit
  170. //
  171. // Draws a scaled sprite to the destination.
  172. //
  173. BOOL CAnimSprite::StretchBlit(int dx, int dy)
  174. {
  175.     return StretchBlt(GetScreenDC(), 
  176.         GetX(), GetY(), dx, dy, 
  177.         sprites[iCurFrame]->GetSourceDC(),
  178.         0, 0, ImageWidth(), ImageHeight(), SRCCOPY);
  179. }
  180.  
  181. //////////////////////////////////////////////////////////////////////
  182. // CAnimSprite::TransBlit
  183. //
  184. // Draws the current sprite frame transparently.
  185. //
  186. BOOL CAnimSprite::TransBlit(COLORREF clrTrans)
  187. {
  188.     return TransparentImage(GetScreenDC(), 
  189.         GetX(), GetY(),
  190.         ImageWidth(), ImageHeight(), 
  191.         sprites[iCurFrame]->GetSourceDC(),0, 0, 
  192.         ImageWidth(), ImageHeight(), clrTrans);
  193. }
  194.  
  195. //////////////////////////////////////////////////////////////////////
  196. // CAnimSprite::SetFrame
  197. //
  198. // Sets the current frame of animation.
  199. //
  200. void CAnimSprite::SetFrame(int iFrame)
  201. {
  202.     if (iFrame >= 0 && iFrame < iTotalFrames)
  203.         iCurFrame = iFrame; 
  204. }
  205.  
  206. //////////////////////////////////////////////////////////////////////
  207. // CAnimSprite::NextFrame
  208. //
  209. // Increments the current frame and accounts for the boundaries.
  210. //
  211. void CAnimSprite::NextFrame()
  212. {
  213.     if (iCurFrame < iTotalFrames - 1)
  214.         iCurFrame++;
  215.     else
  216.         iCurFrame = 0;
  217. }
  218.  
  219. //////////////////////////////////////////////////////////////////////
  220. // CAnimSprite::PrevFrame
  221. //
  222. // Decrements the current frame and accounts for the boundaries.
  223. //
  224. void CAnimSprite::PrevFrame()
  225. {
  226.     if (iCurFrame > 0)
  227.         iCurFrame--;
  228.     else
  229.         iCurFrame = iTotalFrames - 1;
  230. }
  231.  
  232.